<template>
	<view>
		<view class="bg-white padding">
			<view class="u-f-jsb">
				<view>开卡银行</view>
				<picker class="card-input" mode=selector :range="banks"  @change="bindTimeChange">
					<view class="uni-input">{{form.bankType}}</view>
					<u-icon name="arrow-down" color="#666666" size="28"></u-icon>
				</picker>
			</view>
			<view class="u-f-jsb">
				<view>持卡人姓名</view>
				<input class="card-input" placeholder="请输入持卡人姓名" v-model="form.realName"/>
			</view>
			<view class="u-f-jsb">
				<view>银行卡号</view>
				<input type="text" class="card-input"  placeholder="请输入您的卡号" v-model="form.cardNum">
			</view>
		</view>
		<view class="add-card" @click="addcard">确认添加</view>
	</view>
</template>

<script>
	export default{
		data(){
			return{
				banks: [ '工商银行' , '建设银行','农业银行','交通银行','中国银行','招商银行','浦发银行','兴业银行','民生银行','光大银行','广发银行','华夏银行','平安银行','浙商银行','邮政储蓄银行'],                               //银行可选列表
				form:{
					cardNum: '',            //银行卡号
					realName:'',               //银行卡开户人姓名
					bankType:''                //银行
				},
				flag: true                 //放重复提交
			}
		},
		onLoad() {
			this.form.bankType = this.banks[0]
		},
		methods:{
			//银行选择回调
			bindTimeChange(e){
				this.form.bankType = this.banks[e.detail.value]
			},
			// 测试银行卡号 6236682970001339129   6222620640016257054   6215591908001791109
			addcard(){
				//防止重复提交
				if(!this.flag) return this.$tip.toast('手速太快了,悠着点~');
				//数据验证
				if(!this.form.realName) return this.$tip.toast('请输入持卡人姓名！');
				if(!this.$u.test.chinese(this.form.realName)) return this.$tip.toast('请输入正确的持卡人姓名！');   //持卡人姓名必须全为中文
				if(!this.form.cardNum) return this.$tip.toast('请输入银行卡号码！');
				if(!this.luhnCheck(this.form.cardNum)) return this.$tip.toast('银行卡号码错误');  //银行卡号必须遵循银行卡号规则
				this.flag = false;
				//请求接口，添加银行卡
				this.form.busiId = this.$store.state.userInfo.id;
				this.$http.post('/api/auth/business/card/addOrEdit',this.form).then(res => {
					if(!res.success) return this.flag = true;
					this.$tip.successRedirect(res,1500,'../bankCard/bankCard')
				})
			},
			//银行卡验证规则
			luhnCheck(bankno) {
			    var lastNum = bankno.substr(bankno.length - 1, 1); //取出最后一位（与luhn进行比较）
			    var first15Num = bankno.substr(0, bankno.length - 1); //前15或18位
			    var newArr = [];
			    for (var i = first15Num.length - 1; i > -1; i--) { //前15或18位倒序存进数组
			        newArr.push(first15Num.substr(i, 1));
			    }
			    var arrJiShu = []; //奇数位*2的积 <9
			    var arrJiShu2 = []; //奇数位*2的积 >9
			    var arrOuShu = []; //偶数位数组
			    for (var j = 0; j < newArr.length; j++) {
			        if ((j + 1) % 2 == 1) { //奇数位
			            if (parseInt(newArr[j]) * 2 < 9) arrJiShu.push(parseInt(newArr[j]) * 2);
			            else arrJiShu2.push(parseInt(newArr[j]) * 2);
			        } else //偶数位
			        arrOuShu.push(newArr[j]);
			    }
			    var jishu_child1 = []; //奇数位*2 >9 的分割之后的数组个位数
			    var jishu_child2 = []; //奇数位*2 >9 的分割之后的数组十位数
			    for (var h = 0; h < arrJiShu2.length; h++) {
			        jishu_child1.push(parseInt(arrJiShu2[h]) % 10);
			        jishu_child2.push(parseInt(arrJiShu2[h]) / 10);
			    }
			    var sumJiShu = 0; //奇数位*2 < 9 的数组之和
			    var sumOuShu = 0; //偶数位数组之和
			    var sumJiShuChild1 = 0; //奇数位*2 >9 的分割之后的数组个位数之和
			    var sumJiShuChild2 = 0; //奇数位*2 >9 的分割之后的数组十位数之和
			    var sumTotal = 0;
			    for (var m = 0; m < arrJiShu.length; m++) {
			        sumJiShu = sumJiShu + parseInt(arrJiShu[m]);
			    }
			    for (var n = 0; n < arrOuShu.length; n++) {
			        sumOuShu = sumOuShu + parseInt(arrOuShu[n]);
			    }
			    for (var p = 0; p < jishu_child1.length; p++) {
			        sumJiShuChild1 = sumJiShuChild1 + parseInt(jishu_child1[p]);
			        sumJiShuChild2 = sumJiShuChild2 + parseInt(jishu_child2[p]);
			    }
			    //计算总和
			    sumTotal = parseInt(sumJiShu) + parseInt(sumOuShu) + parseInt(sumJiShuChild1) + parseInt(sumJiShuChild2);
			    //计算luhn值
			    var k = parseInt(sumTotal) % 10 == 0 ? 10 : parseInt(sumTotal) % 10;
			    var luhn = 10 - k;
			    if (lastNum == luhn) {
			        // $("#banknoInfo").html("luhn验证通过");
			        return true;
			    } else {
			        // $("#banknoInfo").html("银行卡号必须符合luhn校验");
			        return false;
			    }
			}
			
		}
	}
</script>

<style lang="scss" scoped>
.card-input{
	font-size: 26rpx;
	color: #000;
	width: 60%;
	display: block;
	font-weight: 700;
	margin-bottom: 20rpx;
	.uni-input{
		display: inline-block;
		padding-right: 10rpx;
	}
}

.add-card{
	position: fixed;
	bottom: 20rpx;
	margin: 0 5%;
	width: 90%;
	height: 80rpx;
	text-align: center;
	line-height: 80rpx;
	color: #fff;
	font-size: 15px;
	border-radius: 40rpx;
	background-color: #2ba4f7;
}
</style>
